﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.UC.门店下单.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static DXApplicationTangche.DTO.DTOStyleMeasureItems;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;

namespace DXApplicationTangche.UC.订单一览新.订单预览新.model
{
    public enum Enum订单查询 { 按照订单号查询订单, 按照水洗标查询订单 }
    public class 订单预览新Model
    {
        #region
        private readonly List<StyleAccessoryDTO> styleAccessoryDTOs = new List<StyleAccessoryDTO>();
        public String orderId { get; set; }
        public OrderCommitDTO返回 OrderCommitDTO返回 { get; set; } = new OrderCommitDTO返回();
        public Enum订单类别 @enum { get; set; }
        public List<款式DTOs> 款式DTOs { get; set; } = new List<款式DTOs>();
        public List<面料DTOs> 面料DTOs { get; set; } = new List<面料DTOs>();
        public List<备注DTOs> 备注DTOs { get; set; } = new List<备注DTOs>();
        public List<DTODesignPoints> 设计点DTOs { get; set; } = new List<DTODesignPoints>();
        public DTOStyleMeasureItems 尺寸 { get; set; } = new DTOStyleMeasureItems();
        public StyleCategoryDTO styleCategoryDTO { get; set; } = new StyleCategoryDTO();//款式类别DTO
        public String refBarcode { get; set; }
        #endregion
        public 订单预览新Model(String str, Enum订单查询 @enum) {
            if (@enum.Equals(Enum订单查询.按照订单号查询订单))
            {
                this.orderId = str;
                this.OrderCommitDTO返回 = 订单预览新Service.get订单预览(this.orderId);
                //  只读属性赋值
                this.styleAccessoryDTOs.AddRange(this.OrderCommitDTO返回.styleAccessoryDTOs);
            }
            else {
                this.OrderCommitDTO返回 = 订单预览新Service.getOrderCommitDTOByBarcode(str);//"20201026-a6"          
                this.orderId = this.OrderCommitDTO返回.orderId;
                //  只读属性赋值
                this.styleAccessoryDTOs.AddRange(this.OrderCommitDTO返回.styleAccessoryDTOs);
            }
        }

        #region 抖音
        public 订单预览新Model(String refBarcode)
        {
            this.OrderCommitDTO返回 = 订单预览新Service.getOrderCommitDTOByBarcode(refBarcode);//"20201026-a6"          
            this.orderId = this.OrderCommitDTO返回.orderId;
            this.OrderCommitDTO返回 = OrderCommitDTO返回;
            this.OrderCommitDTO返回.refBarcode = refBarcode;
            this.OrderCommitDTO返回.id = new FunctionHelper().Uuid;
            //  只读属性赋值
            this.styleAccessoryDTOs.AddRange(this.OrderCommitDTO返回.styleAccessoryDTOs);
        }

        public 订单预览新Model buildDouyinRemarks(String remarks)
        {
            this.OrderCommitDTO返回.remarks = remarks;
            return this;
        }
        public 订单预览新Model buildDouyinMaterial(
            String materialNameCn,
            String materialCode,
            String composition
            )
        {
            this.OrderCommitDTO返回.materialNameCn = materialNameCn;
            this.OrderCommitDTO返回.materialCode = materialCode;
            this.OrderCommitDTO返回.composition = composition;
            return this;
        }
        public 订单预览新Model buildDouyinStyleSizeCd(String styleSizeCd)
        {
            if (Regex.Matches(styleSizeCd, "[a-zA-Z]").Count > 0)
            {
                this.OrderCommitDTO返回.styleSizeCd =
                    "IGS_GROUP_SIZE-" + styleSizeCd;
            }
            else
            {
                this.OrderCommitDTO返回.styleSizeCd =
                    "EGS_GROUP_SIZE-" + styleSizeCd;
            }

            this.尺寸 = FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(
                RestCall.httpGetMethod(
                    URL.接口url + "/style/generateStyleMeasureItemsByStyleNameCn?SIZE_CD="
                                + this.OrderCommitDTO返回.styleSizeCd
                                + "&styleNameCn=" + this.OrderCommitDTO返回.styleNameCn.Split()[0]
                                + "&categoryId=" + this.build查询款式类别().styleCategoryDTO.id
                            )
                );
            this.OrderCommitDTO返回.measureItemDTOS = this.尺寸.measureItemDTOS;
            return this;
        }
        public 订单预览新Model buildDouyin()
        {
            this.buildOrderCommit().buildStyleCategoryDTOAndClear();
            this.buildCustomerClear();
            this.OrderCommitDTO返回.salesType = "套号下单";
            this.OrderCommitDTO返回.salesChannel = "春衫定制";
            return this;
        }

        public 订单预览新Model buildDouyin(
            String orderNumber,
            String retailPrice
            )
        {
            this.OrderCommitDTO返回.orderNumber = Convert.ToDouble(orderNumber);
            this.OrderCommitDTO返回.build抖音价格(Convert.ToDouble(retailPrice));
            return this;
        }
        public 订单预览新Model buildDouyinSize(
            String orderNumber,
            String retailPrice
            )
        {
            this.OrderCommitDTO返回.orderNumber = Convert.ToDouble(orderNumber);
            this.OrderCommitDTO返回.build抖音价格(Convert.ToDouble(retailPrice));
            return this;
        }
        #endregion


        public 订单预览新Model select尺寸()
        {
            this.尺寸.sessionId = this.OrderCommitDTO返回.sessionId;
            this.尺寸.measureItemDTOS = this.OrderCommitDTO返回.measureItemDTOS;
            try { this.尺寸.measureItemDTOS.Sort(new DTOMeasureItemIndexComparer()); } catch { }
            return this;
        }
        /// <summary>
        /// orderCommitDTO返回的重新查询
        /// </summary>
        public void refreshOrderCommitDTO返回() {
            this.OrderCommitDTO返回 = 订单预览新Service.get订单预览(this.orderId);
        }
        /// <summary>
        /// 修改辅料
        /// </summary>
        /// <param name="newStyleAccessoryDTOs"></param>
        public void saveAccessory(List<StyleAccessoryDTO> newStyleAccessoryDTOs)
        {
            //this.styleAccessoryDTOs.Sort();
            //newStyleAccessoryDTOs.Sort();
            if (!FunctionHelper.JsonSerialization(this.styleAccessoryDTOs).Equals(FunctionHelper.JsonSerialization(newStyleAccessoryDTOs))
                )
            {
                //  更新订单
                this.OrderCommitDTO返回.styleAccessoryDTOs = newStyleAccessoryDTOs;
                订单预览新Service.post修改辅料(this.orderId, false, this.OrderCommitDTO返回.styleAccessoryDTOs);
            }
        }
        /// <summary>
        /// 页面内容显示
        /// </summary>
        public 订单预览新Model buildOrderCommit()
        {
            if ("18".Equals(this.OrderCommitDTO返回.shopId))
            {
                this.@enum = Enum订单类别.销售订单;
            }
            else if ("50".Equals(this.OrderCommitDTO返回.shopId))
            {
                this.@enum = Enum订单类别.套号订单;
            }
            else if ("48".Equals(this.OrderCommitDTO返回.shopId))
            {
                this.@enum = Enum订单类别.样品订单;
            }
            else if ("46".Equals(this.OrderCommitDTO返回.shopId))
            {
                this.@enum = Enum订单类别.坚山订单;
            }
            else if ("49".Equals(this.OrderCommitDTO返回.shopId))
            {
                this.@enum = Enum订单类别.坚山美国订单;
            }
            else if ("51".Equals(this.OrderCommitDTO返回.shopId))
            {
                this.@enum = Enum订单类别.Luna店铺订单;
            }
            else if ("52".Equals(this.OrderCommitDTO返回.shopId))
            {
                this.@enum = Enum订单类别.坚山上海订单;
            }
            this.尺寸.sessionId = this.OrderCommitDTO返回.sessionId;
            this.尺寸.measureItemDTOS = this.OrderCommitDTO返回.measureItemDTOS;
            this.款式DTOs.Add(new 款式DTOs(this.OrderCommitDTO返回));
            this.面料DTOs.Add(new 面料DTOs(this.OrderCommitDTO返回));
            try
            {
                Dictionary<String, String> keyValuePairs = 订单预览新Service.get备注图(this.OrderCommitDTO返回.orderId);
                foreach (KeyValuePair<String, String> keyValuePair in keyValuePairs)
                {
                    this.备注DTOs.Add(new 备注DTOs(keyValuePair));
                }
            }
            catch { }
            try
            {
                foreach (DTODesignPoints设计点 dTODesignPoints in this.OrderCommitDTO返回.dtoDesignPoints)
                {
                    //if (this.@enum == Enum订单类别.坚山美国订单)
                    //{
                    //    DTODesignPoints设计点 设计点 = 订单预览新Service.select美国设计点(dTODesignPoints);
                    //    dTODesignPoints.itemTypeNameCn = 设计点.itemTypeNameCn;
                    //    dTODesignPoints.name = 设计点.name;
                    //}
                    this.设计点DTOs.Add(new DTODesignPoints(dTODesignPoints));
                }
            }
            catch { }

            //if (this.@enum == Enum订单类别.坚山美国订单)
            //{
            //    this.设计点DTOs = OrderService.get设计点BySYS_STYLE_ID(this.OrderCommitDTO返回.styleId);
            //    this.OrderCommitDTO返回.dtoDesignPoints.Clear();
            //    foreach (DTODesignPoints dTO in this.设计点DTOs)
            //    {
            //        this.OrderCommitDTO返回.dtoDesignPoints.Add(new DTODesignPoints设计点(dTO));
            //    }
            //}

            try { this.尺寸.measureItemDTOS.Sort(new DTOMeasureItemIndexComparer()); } catch { }
            return this;
        }
        //查询款式类别
        public 订单预览新Model buildStyleCategoryDTOAndClear()
        {
            //查询成衣的款式类别
            try {
                if (!String.IsNullOrEmpty(this.refBarcode))
                {
                    this.styleCategoryDTO = 订单预览新Service.getCategoryByBarcode(this.refBarcode);
                }
                else
                {
                    this.styleCategoryDTO = 订单预览新Service.getCategoryByStyleName(this.OrderCommitDTO返回.styleNameCn);
                    //this.styleCategoryDTO = 订单预览新Service.get根据styleId取款式大类别(this.OrderCommitDTO返回.refStyleId);
                }
                //this.styleCategoryDTO = 订单预览新Service.getCategoryByBarcode(this.refBarcode); 
                //this.styleCategoryDTO = 订单预览新Service.get根据styleId取款式大类别(this.OrderCommitDTO返回.refStyleId);
                //this.styleCategoryDTO = 订单预览新Service.get根据StyleCategoryCd取款式大类别(this.OrderCommitDTO返回.styleCategoryCd);
                //if (String.IsNullOrEmpty(this.styleCategoryDTO.id))
                //{
                //    this.styleCategoryDTO = 订单预览新Service.get根据styleId取款式大类别(this.OrderCommitDTO返回.refStyleId);
                //}
            } catch { }
            //与此次下单无关的参考内容删除
            this.备注DTOs.Clear();
            this.OrderCommitDTO返回.remarks = null;
            this.OrderCommitDTO返回.refOrderId = null;
            this.OrderCommitDTO返回.refOrderNo = null;
            return this;
        }
        public 订单预览新Model build查询款式类别()
        {
            //查询成衣的款式类别
            try
            {
                if (!String.IsNullOrEmpty(this.refBarcode))
                {
                    this.styleCategoryDTO = 订单预览新Service.getCategoryByBarcode(this.refBarcode);
                }
                else
                {
                    this.styleCategoryDTO = 订单预览新Service.getCategoryByStyleName(this.OrderCommitDTO返回.styleNameCn);
                    //this.styleCategoryDTO = 订单预览新Service.get根据styleId取款式大类别(this.OrderCommitDTO返回.refStyleId);
                }
                //this.styleCategoryDTO = 订单预览新Service.getCategoryByBarcode(refBarcode);
                //this.styleCategoryDTO = 订单预览新Service.get根据styleId取款式大类别(this.OrderCommitDTO返回.refStyleId);
                //this.styleCategoryDTO = 订单预览新Service.get根据StyleCategoryCd取款式大类别(this.OrderCommitDTO返回.styleCategoryCd);
                //if (String.IsNullOrEmpty(this.styleCategoryDTO.id))
                //{
                //    this.styleCategoryDTO = 订单预览新Service.get根据styleId取款式大类别(this.OrderCommitDTO返回.refStyleId);
                //}
            }
            catch { }
            return this;
        }
        /// <summary>
        /// 门店套号二次下单去掉原客户信息，改成春衫下单
        /// </summary>
        /// <returns></returns>
        public 订单预览新Model buildCustomerClear()
        {
            //原客户信息删除
            this.OrderCommitDTO返回.customerId = null;
            this.OrderCommitDTO返回.customerName = null;
            this.OrderCommitDTO返回.addressId = null;
            this.OrderCommitDTO返回.addressProvince = null;
            this.OrderCommitDTO返回.addressCity = null;
            this.OrderCommitDTO返回.addressDistrict = null;
            this.OrderCommitDTO返回.addressDetail = null;
            this.OrderCommitDTO返回.zipCode = null;
            this.OrderCommitDTO返回.customerCountId = null;
            this.OrderCommitDTO返回.mobile = null;
            this.OrderCommitDTO返回.consignee = null;
            return this;
        }
        /// <summary>
        /// 定制订单扫水洗标下单，删除原先订单的刺绣内容
        /// </summary>
        public void buildClear刺绣()
        {
            //原来的刺绣内容也删除
            try
            {
                for (int i = this.设计点DTOs.Count - 1; i > 0; i--)
                {
                    if (this.设计点DTOs[i].itemValue.Contains("4SMT")
                        || this.设计点DTOs[i].itemValue.Contains("4SMC")
                        || this.设计点DTOs[i].itemValue.Contains("4SMO")
                        || this.设计点DTOs[i].itemValue.Contains("4SMP")
                        || this.设计点DTOs[i].itemValue.Contains("4SMS"))
                    {
                        this.设计点DTOs.Remove(this.设计点DTOs[i]);
                    }
                }
            }
            catch { }
        }
        /// <summary>
        /// 成衣扫码下单给orderCommitDTO相关属性赋值
        /// </summary>
        public void buildModelOrderCommitDTO()
        {
            this.OrderCommitDTO返回.id = new FunctionHelper().Uuid;
            //面料
            this.OrderCommitDTO返回.materialCode = Frm面料选择.mianliaocd;
            this.OrderCommitDTO返回.materialNameCn = Frm面料选择.mianliao;
            this.OrderCommitDTO返回.styleFabricId = Frm面料选择.mianliaoid;
            //尺寸
            this.OrderCommitDTO返回.sessionId = this.尺寸.sessionId;
            this.OrderCommitDTO返回.measureItemDTOS = this.尺寸.measureItemDTOS;
            //设计点
            this.OrderCommitDTO返回.dtoDesignPoints.Clear();
            foreach (DTODesignPoints item in this.设计点DTOs)
            {
                this.OrderCommitDTO返回.dtoDesignPoints.Add(new DTODesignPoints设计点(item));
            }
        }
        /// <summary>
        /// 套号的销售样衣下单，仅选择尺寸及数量，其他不更改
        /// </summary>
        public 订单预览新Model buildSaleSampleOrderCommitDTO()
        {
            this.OrderCommitDTO返回.id = new FunctionHelper().Uuid;
            //尺寸
            this.OrderCommitDTO返回.sessionId = this.尺寸.sessionId;
            this.OrderCommitDTO返回.measureItemDTOS = this.尺寸.measureItemDTOS;
            this.OrderCommitDTO返回.customerId = "6299";
            this.OrderCommitDTO返回.customerCountId = "6436";
            this.OrderCommitDTO返回.customerName = "春衫定制";
            this.OrderCommitDTO返回.addressId = "4969";
            return this;
        }
        /// <summary>
        /// 设计点重新赋值
        /// </summary>
        /// <returns></returns>
        public 订单预览新Model build设计点()
        {
            //设计点
            this.OrderCommitDTO返回.dtoDesignPoints.Clear();
            foreach (DTODesignPoints item in this.设计点DTOs)
            {
                this.OrderCommitDTO返回.dtoDesignPoints.Add(new DTODesignPoints设计点(item));
            }
            return this;
        }
        /// <summary>
        /// 套号的经销商下单，仅选择尺寸及数量，其他不更改
        /// </summary>
        public 订单预览新Model buildDistributorOrderCommitDTO()
        {
            this.OrderCommitDTO返回.id = new FunctionHelper().Uuid;
            //尺寸
            this.OrderCommitDTO返回.sessionId = this.尺寸.sessionId;
            this.OrderCommitDTO返回.measureItemDTOS = this.尺寸.measureItemDTOS;
            return this;
        }
        /// <summary>
        /// orderCommitDTO保存样衣的成衣码和货号主键
        /// </summary>
        /// <param name="refBarcode"></param>
        /// <param name="saleInventoryId"></param>
        /// <returns></returns>
        public 订单预览新Model buildOrderCommitDTOSample(String refBarcode)
        {
            this.OrderCommitDTO返回.refBarcode = refBarcode;
            return this;
        }
        /// <summary>
        /// 电商下单填写地址
        /// </summary>
        /// <param name="consignee"></param>
        /// <param name="addressProvince"></param>
        /// <param name="addressCity"></param>
        /// <param name="addressDistrict"></param>
        /// <param name="addressDetail"></param>
        /// <returns></returns>
        public 订单预览新Model build电商下单(String consignee,String mobile,String addressProvince,String addressCity,String addressDistrict,String addressDetail)
        {
            this.OrderCommitDTO返回.customerId = "6299";
            this.OrderCommitDTO返回.customerCountId = "6436";
            this.OrderCommitDTO返回.customerName = "春衫定制";
            this.OrderCommitDTO返回.addressId = "4969";
            this.OrderCommitDTO返回.consignee = consignee;
            this.OrderCommitDTO返回.mobile = mobile;
            this.OrderCommitDTO返回.addressProvince = addressProvince;
            this.OrderCommitDTO返回.addressCity = addressCity;
            this.OrderCommitDTO返回.addressDistrict = addressDistrict;
            this.OrderCommitDTO返回.addressDetail = addressDetail;
            return this;
        }
        
        /// <summary>
        /// 套号区分不同销售渠道
        /// </summary>
        /// <param name="salesType"></param>
        /// <param name="salesChannel"></param>
        /// <returns></returns>
        public 订单预览新Model build销售渠道(String salesType,String salesChannel)
        {
            this.OrderCommitDTO返回.salesType = salesType;
            this.OrderCommitDTO返回.salesChannel = salesChannel;
            return this;
        }
        /// <summary>
        /// 清空销售渠道相关
        /// </summary>
        /// <returns></returns>
        public 订单预览新Model build销售渠道Clear()
        {
            this.OrderCommitDTO返回.salesType = "";
            this.OrderCommitDTO返回.salesChannel = "";
            return this;
        }

    }

    public class 审版model {
        //审版
        public DTO标准版 DTO标准版 = new DTO标准版();
        public DTO标准版 DTO个人版 = new DTO标准版();

        public 审版model() { 
        }

        public 审版model(OrderCommitDTO返回 OrderCommitDTO返回) {
            if (String.IsNullOrEmpty(OrderCommitDTO返回.refStyleId))
            {
                return;
            }
            this.DTO标准版 = 订单预览新Service.get是否上传标准版(OrderCommitDTO返回.refStyleId);
            this.DTO个人版 = 订单预览新Service.get是否上传个人版(OrderCommitDTO返回.refStyleId, OrderCommitDTO返回.customerId);
        }
    }
}
